import { LoadingController,ToastController, Loading ,Toast } from 'ionic-angular'
import {Http, Headers, RequestOptions, URLSearchParams} from "@angular/http";
import { AppConfig } from '../app/app.config'

export class baseCommon{
    constructor(){}

    static showLoading(loadingCtrl:LoadingController,message:string):Loading{
        let loader = loadingCtrl.create({
            content: message,
            dismissOnPageChange: true,
        })
        loader.present();
        return loader;
    }

    static showToast(toastCtrl:ToastController,message:string):Toast{
        let toast = toastCtrl.create({
            message: message,
            duration: 3000,
            position:'top'
        })
        toast.present();
        return toast;
    }

    /**
     * 获得个人信息
     * 
     * @protected
     * @param {Http} http 
     * @param {any} phone 
     * @param {any} sessionID 
     * @memberof baseCommon
     */
    static getInfo(http:Http,phone,sessionID) {
        let headers = new Headers({
          'Content-Type': 'application/x-www-form-urlencoded',
        });
    
        let options = new RequestOptions({
          headers: headers,
        });
    
        return http.get(AppConfig.httpLoc + '/info/' + phone + "?sessionID=" + sessionID, options)
          .toPromise()
          .then(data => {
            let listData = data.json().data;
            if (listData.sex == 0) {
              listData.sex = '保密';
            } else if(listData.sex == 1){
              listData.sex = '男';
            } else if(listData.sex == 2){
              listData.sex = '女';
            }
            if (listData.job == null) {
              listData.job = '未设置';
            }
            if (listData.hometown == null) {
              listData.hometown = '未设置';
            }
            if (listData.school == null) {
              listData.school = '未设置';
            }
            if (listData.company == null) {
              listData.company = '未设置';
            }
            if (listData.remarks == null) {
              listData.remarks = '未设置';
            }
            return listData;
          }, err => {
            console.log(err);
          })
      }

      /**
       * 得到圈子用户
       * 
       * @param {Http} http 
       * @param {any} groupID 
       * @param {any} sessionID 
       * @returns 
       * @memberof baseCommon
       */
      static getGroupAllUser(http:Http,groupID,sessionID) {
        let headers = new Headers({
          'Content-Type': 'application/x-www-form-urlencoded',
        });
    
        let options = new RequestOptions({
          headers: headers,
        });
    
        return http.get(AppConfig.httpLoc + '/group/users?id=' + groupID + "&sessionID=" + sessionID, options)
          .toPromise()
          .then(data => {
            return data.json().data;
          }, err => {
            console.log(err);
          })
      }
}